import uuid
import json
from datetime import datetime
from flask import g, jsonify, request
from flask_jwt_extended import jwt_required, current_user
from myapp import db
from myapp.routes import system
from myapp.models import Organization
from myapp.utils.auth import permission

@permission('system:dept:list')
@system.route('/dept/list', methods=['GET'])
@jwt_required()
def syorganization_treeGrid():
    filters = []
    if 'deptName' in request.args:
        filters.append(Organization.name.like('%' + request.args['deptName'] + '%'))

    orgs = Organization.query.filter(*filters)

    return jsonify({'msg': '操作成功', 'code': 200, "data": [org.to_json() for org in orgs]})

@system.route('/dept/treeselect', methods=['GET'])
@jwt_required()
def syorganization_tree_select():
    orgs = Organization.query.filter(Organization.organization_id == None)

    return jsonify({'msg': '操作成功', 'code': 200, "data": [org.to_tree_select_json() for org in orgs]}) 

@system.route('/dept/list/exclude/<id>', methods=['GET'])
@jwt_required()
def syorganization_dept_list_exclude(id):
    orgs = Organization.query.filter(Organization.id != id)

    return jsonify({'msg': '操作成功', 'code': 200, "data": [org.to_json() for org in orgs]})

@system.route('/dept/<string:id>', methods=['GET'])
@permission('system:dept:query')
@jwt_required()
def syorganization_getById(id):
    org = Organization.query.get(id)

    if org:
        return jsonify({'msg': '操作成功', 'code': 200, 'data': org.to_json()})
    else:
        return jsonify({'success': False, 'msg': 'error'})

@system.route('/dept', methods=['PUT'])
@permission('system:dept:edit')
@jwt_required()
def syorganization_update():
    org = Organization.query.get(request.json['deptId'])

    org.update_time = datetime.now()
    if 'deptName' in request.json:  org.name = request.json['deptName']
    if 'email' in request.json: org.email = request.json['email']
    if 'leader' in request.json: org.leader = request.json['leader']
    if 'phone' in request.json: org.phone = request.json['phone']
    if 'orderNum' in request.json: org.seq = request.json['orderNum']
    if 'parentId' in request.json: org.parent = Organization.query.get(request.json['parentId'])
    if 'status' in request.json: org.status = request.json['status']

    db.session.add(org)

    return jsonify({'code': 200, 'msg': '操作成功'})

@permission('system:dept:add')
@system.route('/dept', methods=['POST'])
@jwt_required()
def syorganization_save():
    org = Organization()
    org.ID = str(uuid.uuid4())
    if 'deptName' in request.json:  org.name = request.json['deptName']
    if 'email' in request.json: org.email = request.json['email']
    if 'leader' in request.json: org.leader = request.json['leader']
    if 'phone' in request.json: org.phone = request.json['phone']
    if 'orderNum' in request.json: org.seq = request.json['orderNum']
    if 'parentId' in request.json: org.parent = Organization.query.get(request.json['parentId'])
    if 'status' in request.json: org.status = request.json['status']

    # add organization to current user
    current_user.organizations.append(org)

    db.session.add(org)

    return jsonify({'code': 200, 'msg': '操作成功'})

@permission('system:dept:remove')
@system.route('/dept/<string:id>', methods=['DELETE'])
@jwt_required()
def sydept_delete(id):
    org = Organization.query.get(id)
    if org:
        db.session.delete(org)

    return jsonify({'code': 200, 'msg': '操作成功'})
